package at.fhooe.SRWCProject.WiFiDirect.Socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.Handler;
import at.fhooe.SRWCProject.Bluetooth.ChatActivity;
import at.fhooe.SRWCProject.MainActivity;

public class AcceptThread extends Thread {
	private final Handler mmHandler;
	private ServerSocket mmServerSocket;
	private Socket mmSocket = null;

	public AcceptThread(Handler handler) {
		mmHandler = handler;
		try {
			mmServerSocket = new ServerSocket(MainActivity.PORT);
		} catch (IOException e) {
			mmServerSocket = null;
			e.printStackTrace();
		}
	}

	public void run() {
		while (mmServerSocket != null) {
			try {
				mmSocket = mmServerSocket.accept();
				ConnectionThread conn = new ConnectionThread(mmSocket,
						mmHandler);
				mmHandler.obtainMessage(MainActivity.SOCKET_CONNECTED, conn)
						.sendToTarget();
				conn.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}